
#include "stdafx.h"

#include "BoostFileSystem/BoostFileSystem.h"

#include <boost/filesystem.hpp>

#include <gtest/gtest.h>

const char g_rootPath[] = "BoostFileSystemFixture";

class BoostFileSystemFixture : public ::testing::Test
{
public:
	virtual void SetUp()
	{
		boost::filesystem::remove_all(g_rootPath);
		boost::filesystem::create_directory(g_rootPath);
	}

	virtual void TearDown()
	{
		boost::filesystem::remove_all(g_rootPath);
	}
};

TEST_F(BoostFileSystemFixture, doesDirectoryExist)
{
	BoostFileSystem fs(g_rootPath);

	EXPECT_FALSE(fs.doesDirectoryExist("test"));
}

TEST_F(BoostFileSystemFixture, createDirectory)
{
	BoostFileSystem fs(g_rootPath);

	ftp::FileSystemError error = ftp::kSuccess;
	fs.createDirectory("test", &error);

	EXPECT_EQ(ftp::kSuccess, error);
	EXPECT_TRUE(fs.doesDirectoryExist("test"));
}

TEST_F(BoostFileSystemFixture, createDirectoryWithBadPath)
{
	BoostFileSystem fs(g_rootPath);

	ftp::FileSystemError error  = ftp::kSuccess;
	fs.createDirectory("test1/test", &error);
	
	EXPECT_EQ(ftp::kFileOrPathDoesNotExist, error);
}

TEST_F(BoostFileSystemFixture, removeDirectoryWithBadPath)
{
	BoostFileSystem fs(g_rootPath);

	ftp::FileSystemError error = ftp::kSuccess;
	fs.removeDirectory("test", &error);
	
	EXPECT_EQ(ftp::kFileOrPathDoesNotExist, error);

	error = ftp::kSuccess;
	fs.removeDirectory("test/test", &error);
	
	EXPECT_EQ(ftp::kFileOrPathDoesNotExist, error);
}

TEST_F(BoostFileSystemFixture, removeDirectory)
{
	BoostFileSystem fs(g_rootPath);
	ftp::FileSystemError error = ftp::kSuccess;

	fs.createDirectory("test", &error);
	fs.removeDirectory("test", &error);

	const bool exists = fs.doesDirectoryExist("test");
	EXPECT_EQ(ftp::kSuccess, error);
	EXPECT_FALSE(exists);
}

TEST_F(BoostFileSystemFixture, renameWithBadPath)
{
	BoostFileSystem fs(g_rootPath);
	ftp::FileSystemError error = ftp::kSuccess;

	fs.rename("test", "test1", &error);

	EXPECT_EQ(ftp::kFileOrPathDoesNotExist, error);
}

TEST_F(BoostFileSystemFixture, rename)
{
	BoostFileSystem fs(g_rootPath);
	ftp::FileSystemError error = ftp::kSuccess;

	fs.createDirectory("test", &error);
	fs.rename("test", "test1", &error);

	EXPECT_EQ(ftp::kSuccess, error);
	EXPECT_FALSE(fs.doesDirectoryExist("test"));
	EXPECT_TRUE(fs.doesDirectoryExist("test1"));
}

TEST_F(BoostFileSystemFixture, readDirectoryWithBadPath)
{
	BoostFileSystem fs(g_rootPath);
	ftp::FileSystemError error = ftp::kSuccess;
	std::vector<std::string> files;

	fs.readDirectory("test", &files, &error);

	EXPECT_EQ(ftp::kFileOrPathDoesNotExist, error);
	EXPECT_TRUE(files.empty());
}

TEST_F(BoostFileSystemFixture, readDirectoryWithEmptyRoot)
{
	BoostFileSystem fs(g_rootPath);
	ftp::FileSystemError error = ftp::kSuccess;
	std::vector<std::string> files;

	fs.readDirectory("/", &files, &error);

	EXPECT_EQ(ftp::kSuccess, error);
	EXPECT_TRUE(files.empty());
}

TEST_F(BoostFileSystemFixture, readDirectoryWithItems)
{
	BoostFileSystem fs(g_rootPath);
	ftp::FileSystemError error = ftp::kSuccess;
	std::vector<std::string> files;

	fs.createDirectory("test", &error);
	fs.createDirectory("test1", &error);
	fs.createDirectory("test2", &error);

	fs.readDirectory("/", &files, &error);

	EXPECT_EQ(ftp::kSuccess, error);
	ASSERT_EQ(3, files.size());

	EXPECT_EQ("test", files[0]);
	EXPECT_EQ("test1", files[1]);
	EXPECT_EQ("test2", files[2]);
}
